/*
 * Copyright (c) 2015, FPX and/or its affiliates. All rights reserved.
 * Use, Copy is subject to authorized license.
 */

package com.commerceerp.system.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * http 客户端 工具类
 *
 * @author hepx
 * @date 2015/12/11
 */
public class HttpClientUtil
{


    /**
     * POST JSON数据
     *
     * @param url
     * @param jsonData
     * @return
     * @throws IOException
     * @throws IOException
     */
    public static String postJson(String url, String jsonData) throws IOException {
        HttpPost post = new HttpPost(url);
        post.addHeader("Content-Type", "application/json");
        if (StringUtils.isNotEmpty(jsonData)) {
            post.setEntity(new StringEntity(jsonData, Charset.forName("UTF-8")));
        }
        CloseableHttpResponse response = HttpClients.createDefault().execute(post);
        System.out.println(url);
        if (response.getStatusLine().getStatusCode() != 200) {
            System.out.println("状态错误");
        }
        return EntityUtils.toString(response.getEntity());
    }

}
